package com.jy.adscm.utils;

import com.jy.adscm.exception.AdscmSystemException;
import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yyy
 * @ClassName AdscmDateUtils
 * @Description 时间工具类
 * @date 2018/8/13 17:24
 */
public class AdscmDateUtils {

    /**
     * 得到指定日期所在的周是从那天开始 到哪天结束的，是多少周
     * @param time
     * @return
     * @throws ParseException
     */
    public static Map<String, Object> getWeekByDate(Date time) throws ParseException {
        Map<String, Object> map=new HashMap<String, Object>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
        cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        String imptimeBegin = sdf.format(cal.getTime());
        System.out.println("所在周星期一的日期：" + imptimeBegin);
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = sdf.format(cal.getTime());
        System.out.println("所在周星期日的日期：" + imptimeEnd);
//        if (time.getTime()>=sdf.parse(imptimeBegin).getTime()&&cal.getTime().getTime()<=sdf.parse(imptimeEnd).getTime()) {
            map.put("benginDate", imptimeBegin);
            map.put("endDate", imptimeEnd);
            map.put("whereWeek", cal.get(Calendar.WEEK_OF_YEAR));
//        }
        return map;
    }
    public static int getWeek(Date time,Date time1) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }

        int a=cal.get(Calendar.WEEK_OF_YEAR);
        cal.setTime(time1);
        int b=cal.get(Calendar.WEEK_OF_YEAR);


        return a-b;
    }

    /**
     * 判断指定日期是哪一周
     * @param time
     * @return
     * @throws ParseException
     */
    public static int getWeek(Date time) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }

        int a=cal.get(Calendar.WEEK_OF_YEAR);

        return a;
    }

    /**
     * 获取指定日期的前或者后num年/月/天，根据类型
     * @param type : 类型：type = Calendar.YEAR、Calendar.MONTH、Calendar.DATE
     * @return : yyyy-MM-dd
     */
    public static String getThatDateStr(Date date, int num, int type){
        return AdscmDateUtils.getNowOnlyDateFomat1(getThatDate(date,num,type));
    }
    /**
     * 获取指定日期的前或者后num年/月/天，根据类型
     * @param type : 类型：type = Calendar.YEAR、Calendar.MONTH、Calendar.DATE
     * @return : Date
     */
    public static Date getThatDate(Date date, int num, int type){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(type, num);
        date = calendar.getTime();
        return date;
    }
    /**
     * 获取指定日期当天的最后一秒钟
     * @return : Date
     */
    public static Date getThatDateTime(Date date){
        Date newDate = null;
        try{
            String d = new SimpleDateFormat("yyyyMMdd").format(date) + " 23:59:59";
            newDate = new SimpleDateFormat("yyyyMMdd HH:mm:ss").parse(d);
        }catch (Exception e){}
        return newDate;
    }

    /**
     * 将制定格式的字符串转换成日期，字符串格式：yyyy-MM-dd
     * @return
     */
    public static Date parseDate(String dateStr) throws AdscmSystemException{

        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
        } catch (ParseException e) {
            throw new AdscmSystemException(e.getMessage());
        }
        return date;
    }
    /**
     * 将制定格式的字符串转换成日期，字符串格式：yyyyMMdd
     * @return
     */
    public static Date parseDate2(String dateStr) throws AdscmSystemException{

        Date date = null;
        try {
            date = new SimpleDateFormat("yyyyMMdd").parse(dateStr);
        } catch (ParseException e) {
            throw new AdscmSystemException(e.getMessage());
        }
        return date;
    }
    /**
     * 格式化指定日期，格式：yyyy-MM-dd
     * @return
     */
    public static String getNowOnlyDateFomat1(Date date){
        String dfs = new SimpleDateFormat("yyyy-MM-dd").format(date);
        return dfs;
    }
    /**
     * 格式化指定日期，格式：yyyyMMdd
     * @return
     */
    public static String getNowOnlyDateFomat2(Date date){
        String dfs = new SimpleDateFormat("yyyyMMdd").format(date);
        return dfs;
    }

    /**
     * 格式化指定日期，格式：yyMMdd
     * @return
     */
    public static String getNowOnlyDateFomat3(Date date){
        String dfs = new SimpleDateFormat("yyMMdd").format(date);
        return dfs;
    }
    /**
     * 格式化指定日期，格式：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getDateFomat(Date date){
        String dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        return dfs;
    }

    /**
     * 获取两个日期之间的间隔的每一天
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getBetweenDate(Date startDate,Date endDate){

        List<String> list = new ArrayList<>();
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String sdStr = format.format(startDate);
            String edStr = format.format(endDate);

            Date sdDate = format.parse(sdStr);
            Date edDate = format.parse(edStr);

            while (edDate.getTime() > sdDate.getTime()){
                list.add(AdscmDateUtils.getNowOnlyDateFomat2(sdDate));
                sdDate = AdscmDateUtils.getThatDate(sdDate,1,Calendar.DATE);
            }
            list.add(AdscmDateUtils.getNowOnlyDateFomat2(edDate));
        }catch (Exception e){
            e.printStackTrace();
        }

        return list;
    }
    /**
     * 传入一个时间，得到当前月的第一天 比如2018-06-01
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfTheMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 传入一个时间，得到当前月的最后一天 比如2018-06-30
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfTheMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH,
                calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }
    /**
     * 传入一个时间，得到年
     *
     * @param date
     * @return
     */
    public static int getTheYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }
    /**
     * 传入一个时间，得到月
     *
     * @param date
     * @return
     */
    public static int getTheMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     *根据一段日期算出中间是否有多少个周末
     * @param startDate
     * @param endDate
     * @return
     * @throws Exception
     */
    public  static  int getSunday(Date startDate,Date endDate) {
        int temp= 0;
        try {
            temp = 0;
            List<String> betweenDate = getBetweenDate(startDate, endDate);
            for (String date : betweenDate) {
                Date tempdate = parseDate2(date);
                boolean isSunDay = getIsSunDay(tempdate);
                if (isSunDay){
                    temp++;
                }
            }
        } catch (AdscmSystemException e) {
            e.printStackTrace();
        }

        return temp;
    }

    /**
     * 判断是否是周末
     * @param date
     * @return
     */
 public static boolean getIsSunDay( Date date){
        boolean result=false;
      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
     int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
     if (w < 0)
         w = 0;
     if (w==0){
       return  true;
     }
        return result;
 }    
 public static Date getFormateDate( Date date){
     Date parse = null;
     try {
         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
         String format = dateFormat.format(date);
         parse = dateFormat.parse(format);
     } catch (ParseException e) {
         e.printStackTrace();
     }

     return parse;
 }
    /**
     * 由出生日期计算年龄
     * @param birthDay
     * @return
     * @throws Exception
     */
    public int getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else{
                age--;
            }
        }
        return age;
    }
    /**
     * 判断两个日期是否是同一天
     * @param day1
     * @param day2
     * @return
     * @throws Exception
     */
    public static boolean isSameDay(Date day1,Date day2) {

        return DateUtils.isSameDay(day1, day2);
    }



    public static void main(String[] args) throws Exception{
       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println(getBetweenDate(format.parse("2018-08-28 23:23:23"),format.parse("2018-09-02 23:13:23")));
        int sunday = getSunday(format.parse("2019-01-02"), format.parse("2019-01-08"));
        System.out.println(sunday);
    }
}
